using System;
using System.Collections;

using GURUCORE.Framework.Core.Data;
using GURUCORE.Framework.Business;
using GURUCORE.Framework.DataAccess.ORMapping;
using GURUCORE.Framework.DataAccess.ORMapping.CriteriaMapping;

using GURUCORE.Portal.Service.Business.DTO;
using GURUCORE.Framework.Business.DTO;

namespace GURUCORE.Portal.Service.Business
{
	
	public class PortalConfigureService : BusinessService, IPortalConfigureService
	{		
		[TransactionBound]
		public virtual VwModuleUsageDTO[] GetUsedModule(string p_sWebsiteUniqueID)
		{
			TableMapper<VwModuleUsageDTO> vwModuleUsage = new TableMapper<VwModuleUsageDTO>();

			Expression expWebsiteID = new Expression(VwModuleUsageDTO.WEBSITEUNIQUEID_FLD, Operator.Eq,p_sWebsiteUniqueID);
			Criteria crtFilter = new Criteria(expWebsiteID, null);
			DTOCollection<VwModuleUsageDTO> arrModuleUsage = vwModuleUsage.GetObjects(crtFilter);
			return arrModuleUsage.ToArray();
		}

		

		[TransactionBound]
		public virtual VwLayoutDTO[] GetUsedLayout(string p_sWebsiteUniqueID)
		{
			TableMapper<VwLayoutDTO> tblLayout = new TableMapper<VwLayoutDTO>();
			Expression expWebsiteID = new Expression(VwLayoutDTO.WEBSITEUNIQUEID_FLD, Operator.Eq, p_sWebsiteUniqueID);
			Criteria crtFilter = new Criteria(expWebsiteID, null);
			DTOCollection<VwLayoutDTO> arrLayout = tblLayout.GetObjects(crtFilter);
			return arrLayout.ToArray();
		}
        
        
        [TransactionBound]
        public virtual VwLayoutDTO[] GetUsedLayout(string p_sWebsiteUniqueID, int p_nThemeID)
        {
            TableMapper<VwLayoutDTO> tblLayout = new TableMapper<VwLayoutDTO>();
            Expression expWebsiteID =
                new Expression(VwLayoutDTO.WEBSITEUNIQUEID_FLD, Operator.Eq, p_sWebsiteUniqueID)
                *
                new Expression(VwLayoutDTO.THEMEID_FLD, Operator.Eq, p_nThemeID);

            Criteria crtFilter = new Criteria(expWebsiteID, null);
            DTOCollection<VwLayoutDTO> arrLayout = tblLayout.GetObjects(crtFilter);
            return arrLayout.ToArray();
        }
        
		
		[TransactionBound]
		public virtual VwPageletFrameDTO[] GetUsedPageletFrame(string p_sWebsiteUniqueID)
		{
			TableMapper<VwPageletFrameDTO> tblTheme = new TableMapper<VwPageletFrameDTO>();
			Expression expWebsiteID = new Expression(VwPageletFrameDTO.WEBSITEUNIQUEID_FLD, Operator.Eq, p_sWebsiteUniqueID);
			Criteria crtFilter = new Criteria(expWebsiteID, null);
			DTOCollection<VwPageletFrameDTO> arrPageletFrame = tblTheme.GetObjects(crtFilter);
			return arrPageletFrame.ToArray();
		}

		
		[TransactionBound]
		public virtual VwThemeDTO[] GetUsedTheme(string p_sWebsiteUniqueID)
		{
			TableMapper<VwThemeDTO> tblTheme = new TableMapper<VwThemeDTO>();
			Expression expWebsiteID = new Expression( VwThemeDTO.WEBSITEUNIQUEID_FLD, Operator.Eq, p_sWebsiteUniqueID);
			Criteria crtFilter = new Criteria(expWebsiteID, null);
			DTOCollection<VwThemeDTO> arrTheme = tblTheme.GetObjects(crtFilter);
			return arrTheme.ToArray();
		}
		
		[TransactionBound]
		public virtual VwPageDefinitionDTO[] GetUsedPageDefinition(string p_sWebsiteUniqueID)
		{
			TableMapper<VwPageDefinitionDTO> tblPageDefinition = new TableMapper<VwPageDefinitionDTO>();
			Expression expWebsiteID = new Expression( VwPageDefinitionDTO.WEBSITEUNIQUEID_FLD, Operator.Eq, p_sWebsiteUniqueID);
			Criteria crtFilter = new Criteria(expWebsiteID, null);
			DTOCollection<VwPageDefinitionDTO> arrPageDefinition = tblPageDefinition.GetObjects(crtFilter);
			return arrPageDefinition.ToArray();
		}

		[TransactionBound]
		public virtual VwNavigationDTO[] GetNavigation(string p_sWebsiteUniqueID)
		{
			TableMapper<VwNavigationDTO> tblNavigation = new TableMapper<VwNavigationDTO>();
			Expression expWebsiteID = new Expression( VwPageDefinitionDTO.WEBSITEUNIQUEID_FLD, Operator.Eq,	p_sWebsiteUniqueID);
			Criteria crtFilter = new Criteria(expWebsiteID, null);
			DTOCollection<VwNavigationDTO> arrNavigation = tblNavigation.GetObjects(crtFilter);
			return arrNavigation.ToArray();
		}

		[TransactionBound]
		public virtual LocaleDTO[] GetSupportedLocale()
		{
			TableMapper<LocaleDTO> tblLocale = new TableMapper<LocaleDTO>();
			DTOCollection<LocaleDTO> arrLocale = tblLocale.GetObjects(null);
			return arrLocale.ToArray();
		}
                
        [TransactionBound]
        public virtual URLMapperDTO[] GetURLMapper(bool p_bPublished)
        {
            // get published MAPPER
            Expression expPublished = new Expression( URLMapperDTO.PUBLISH_FLD, Operator.Eq,
                p_bPublished ? 1 : 0);            

            TableMapper<URLMapperDTO> tbl = new TableMapper<URLMapperDTO>();
            DTOCollection<URLMapperDTO> arr = tbl.GetObjects(new Criteria(expPublished, null));
            return arr.ToArray();
        }

        public virtual URLMapperDTO[] GetURLMapperPublished()
        {
            return GetURLMapper(true);
        }
        
        [TransactionBound]
        public virtual int TestConnectToDB(string p_sWebsiteUniqueID)
        {
            //TableMapper<VwNavigationDTO> tbl = new TableMapper<VwNavigationDTO>();
            //Expression expWebsiteID = new Expression(VwPageDefinitionDTO.WEBSITEUNIQUEID_FLD, Operator.Eq, p_sWebsiteUniqueID);
            //Criteria crtFilter = new Criteria(expWebsiteID, null);
            //object ret = tbl._GetAggregateValue(_GetObjectsBySqlQueryAsGMetaDTO(
            //    typeof(Int32), Expression.TRUE);
            //return ret != null ? 0 : -1;            


            // TODO: LockeVN: make test function, export interface, test in website application start
            return 0;
        }


	}
}
